
var app = getApp()
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: "获取验证码",
    currentTime: 61,
    isShow: false,
    sheng: "省",
    maskShow: false,
    tel: "",   //手机号
    code: "",  //验证码
    wxnum: "", //微信号
    carnum: "",  //车牌号
    cityName: [
      { city: "京" },
      { city: "津" },
      { city: "渝" },
      { city: "沪" },
      { city: "冀" },
      { city: "晋" },
      { city: "辽" },
      { city: "吉" },
      { city: "黑" },
      { city: "苏" },
      { city: "浙" },
      { city: "皖" },
      { city: "闽" },
      { city: "赣" },
      { city: "鲁" },
      { city: "豫" },
      { city: "鄂" },
      { city: "湘" },
      { city: "粤" },
      { city: "琼" },
      { city: "川" },
      { city: "贵" },
      { city: "云" },
      { city: "陕" },
      { city: "甘" },
      { city: "青" },
      { city: "蒙" },
      { city: "桂" },
      { city: "宁" },
      { city: "新" },
      { city: "藏" },
      { city: "使" },
      { city: "领" },
      { city: "警" },
      { city: "学" },
      { city: "港" },
      { city: "澳" }
    ]
  },
  // 获取验证码
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  // 点击获取验证码
  getVerificationCode: function () {
    var that = this
    let tel = this.data.tel;
    if (tel == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: "none",
        duration: 1000,
        mask: true
      })
      return;
    }
    wx.request({
      url: app.d.severUrl + '/Member/getSmsCode',
      data: {
        openid: app.d.openId,
        mobile: tel,
        type: 1,
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 1000,
            mask: true
          })
        } else {
          that.getCode();
          that.setData({
            disabled: true
          })

        }
      }
    })
  },
  // 选择省份
  changeSele: function (e) {
    const index = e.currentTarget.dataset.index;
    var targetVal = e.currentTarget.dataset.text;
    this.data.sheng = targetVal;
    this.data.isShow = false;
    this.setData({
      sheng: this.data.sheng,
      isShow: this.data.isShow
    })
  },
  showSelect: function () {
    this.data.isShow = true;
    this.setData({
      isShow: this.data.isShow
    })
  },
  closeSelect: function () {
    this.data.isShow = false;
    this.setData({
      isShow: this.data.isShow
    })
  },
  confirm: function () {
    this.data.maskShow = false;
    this.setData({
      maskShow: this.data.maskShow
    })
  },
  // 获取输入的手机号码
  telinput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  // 获取输入的验证码
  codeinput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 获取输入的车牌号
  carnuminput: function (e) {
    this.setData({
      carnum: e.detail.value
    })
  },
  // 确认修改
  bindBtn: function () {
    let tel = this.data.tel;
    let code = this.data.code;
    let carnum = this.data.carnum;
    let sheng = this.data.sheng;
    if (tel == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: "none",
        duration: 1000,
        mask: true
      })
      return;
    } else if (code == "") {
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 1000,
        mask: true
      })
      return; 
    } else if (carnum == "") {
      wx.showToast({
        title: '请输入车牌号',
        icon: "none",
        duration: 1000,
        mask: true
      })
      return;
    } else if (sheng == "省") {
      wx.showToast({
        title: '请选择省份',
        icon: "none",
        duration: 1000,
        mask: true
      })
      return;
    } else {
      console.log(app.d.getMemberData)
      console.log(app.d)
      wx.request({
        url: app.d.severUrl + '/Member/mod_car_code',
        data: {
          member_id: app.d.getMemberData.member_id,
          tel: tel,
          card: sheng + carnum,
          code: code,
        },
        method: 'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 1000,
              mask: true
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 1000,
              mask: true
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000)
          }
        }
      })

    }


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})